﻿using NCP.Core.DataUtil;
using NCP.Model;
using NCP.Util;
using SqlSugar;
using SqlSugar.Extensions;

namespace NCP.Core
{
    public class MemberUtil
    {
        public static Member CurrentMember
        {
            get
            {
                return GetCurrentMember();
            }
        }

        public static Member GetCurrentMember()
        {
            object obj = System.Web.HttpContext.Current.Session[Keys.User.ToString()];
            if (obj != null)
            {
                return obj as Member;
            }
            string userId = EncryptionDes.Decrypt(System.Web.HttpContext.Current.Request.Cookies[Keys.UserID.ToString()].Value);
            if (!string.IsNullOrEmpty(userId))
            {
                SqlSugarClient _client = DbHelper.GetSqlSugarClient();
                Member member = _client.Queryable<Member>().Where(t => t.Id == userId.ObjToInt()).First();
                if (member != null)
                {
                    System.Web.HttpContext.Current.Session[Keys.User.ToString()] = member;
                    System.Web.HttpContext.Current.Session[Keys.UserID.ToString()] = member.Id;
                    return member;
                }
            }
            return null;
        }
    }
}
